package org.example.fancystudy.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author chaigq
 * @version 1.0
 * @date 2022/11/10 22:41
 */
public class InterruptDemo4 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread().getName() + "被中断");
                    break;
                }
                // 当前线程睡眠
                try {
                    Thread.sleep(2);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("-----hello InterruptDemo3");
            }

        }, "thread");
        thread.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        new Thread(() -> {
            //中断 调用sleep方法的线程
            thread.interrupt();
        }, "thread2").start();
    }
}
